package prassein.rsm.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.common.utils.Utility;
import prassein.rsm.dto.DownloadChoice;
import prassein.rsm.dto.UserDto;
import prassein.rsm.entity.base.PermLevel;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.entity.base.User;
import prassein.rsm.service.base.PermLevelManager;
import prassein.rsm.service.base.UserManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "success",location="userList.jsp"), 
	@Result(name = "error",location="login.jsp"),
	@Result(name = "input",location="userList.jsp")})
public class UserListAction extends ActionSupport implements ServletContextAware{
	
	private static SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
	
	@Autowired
	private UserManager userManager;
	
	private PermLevelManager permLevelManager;
	
	private static final long serialVersionUID = -7416211125815929519L;
	
	private ServletContext context;
	
	private long resumeId = -1;
	
	public int totalCount = 0;
	
	private Resume resume = new Resume();

	private String xlsFileLocatoin;
	
	private List<UserDto> userList = new ArrayList<UserDto>();
	
	private DownloadChoice filter = new DownloadChoice();
	
	@Override 
	public String execute() throws Exception {
		initEvn();
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {
			ListAllUser();
			
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	private String ListAllUser() {
		List<User> userListTemp = userManager.getAllUser();
		
		totalCount = userListTemp.size();
		
		for (User item : userListTemp) {
			PermLevel perm = permLevelManager.getPermLevel(item.getPermLevel());
			UserDto userDto = new UserDto();
			userDto.setId(item.getId());
			userDto.setUserName(item.getUserName());
			if (perm != null) {
				userDto.setPerm(Utility.GetPermString(perm.getResumePerm()));
			}
			userList.add(userDto);
		}
		
		return SUCCESS;
	}
	
	private void initEvn() {
		xlsFileLocatoin = "";
	}
	
	public long getResumeId() {
		return resumeId;
	}
	public void setResumeId(long resumeId) {
		this.resumeId = resumeId;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public Resume getResume() {
		return resume;
	}
	public void setResume(Resume resume) {
		this.resume = resume;
	}
	public String getXlsFileLocatoin() {
		return xlsFileLocatoin;
	}
	public void setXlsFileLocatoin(String xlsFileLocatoin) {
		this.xlsFileLocatoin = xlsFileLocatoin;
	}
	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
	public DownloadChoice getFilter() {
		return filter;
	}
	public void setFilter(DownloadChoice filter) {
		this.filter = filter;
	}
	public List<UserDto> getUserList() {
		return userList;
	}
	public void setUserList(List<UserDto> userList) {
		this.userList = userList;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public PermLevelManager getPermLevelManager() {
		return permLevelManager;
	}

	public void setPermLevelManager(PermLevelManager permLevelManager) {
		this.permLevelManager = permLevelManager;
	}

}
